package com.iplant.basic.controller.sfc;

import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.FMCService;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.sfc.SFCFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/SFCFile")
public class SFCFileController extends BasicController<SFCFile> {
    private static final Logger logger = LoggerFactory.getLogger(SFCFileController.class);

    public SFCFileController() {
        super(SFCFile.class);
    }

    @Autowired
    FMCService mSFCFrockBindService;

    /**
     * 上传文件
     */
    @PostMapping("/UploadFile")
    public Object UploadFile(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            SFCFile wSFCFile = CloneTool.Clone(wParam.get("data"), SFCFile.class);
            if (wSFCFile == null || StringUtils.isEmpty(wSFCFile.FileUrl) || wSFCFile.FileClassification <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mSFCFrockBindService.SFC_UploadFile(wLoginUser, wSFCFile);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 删除文件
     */
    @GetMapping("/DeleteFile")
    public Object DeleteFile(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wFileID = StringUtils.parseInt(request.getParameter("FileID"));
            if (wFileID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mSFCFrockBindService.SFC_DeleteFile(wLoginUser, wFileID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
